#include "stdafx.h"
#include <MyViews/OffscreenView.h>

#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif

OffscreenOwner::OffscreenOwner(OffscreenView & view) :
	m_View(view)
{
}

OffscreenOwner::~OffscreenOwner()
{
}

void OffscreenOwner::OnSize(int cx, int cy)
{
	if (m_Bmp.m_hObject != 0)
		m_Bmp.DeleteObject();

	CClientDC dc(&m_View);
	VERIFY(m_Bmp.CreateCompatibleBitmap(&dc, cx, cy));
	if (m_DC.m_hDC == 0)
		VERIFY(m_DC.CreateCompatibleDC(&dc));

	m_DC.SelectObject(&m_Bmp);
}

/////////////////////////////////////////////////////////////////////////////
// OffscreenView

#pragma warning(push)
#pragma warning(disable: 4355) // 'this' used in base member initializer list

OffscreenView::OffscreenView() :
	m_DefaultManager(* this)
{
}

#pragma warning(pop)

OffscreenView::~OffscreenView()
{
}

CDC * OffscreenView::GetOffscreenDC()
{
	return GetOffscreenManager().GetOffscreenDC();
}

void OffscreenView::HandleSize(UINT nType, int cx, int cy) 
{
	BasicView::HandleSize(nType, cx, cy);
	GetOffscreenManager().OnSize(cx, cy);
}

void OffscreenView::HandlePaint() 
{
	CRgn updateRgn;
	VERIFY(updateRgn.CreateRectRgn(0, 0, 0, 0));
	VERIFY(GetUpdateRgn(&updateRgn));

	CPaintDC dc(this);

	if (GetDoubleBuffering()) {
		CRect box;
		VERIFY(updateRgn.GetRgnBox(&box));
		
		CDC * pDC = GetOffscreenDC();
		DoPaint(pDC, updateRgn);

		dc.BitBlt(box.left, box.top, box.Width(), box.Height(), 
			pDC, box.left, box.top, SRCCOPY);
	} else {		 
		DoPaint(&dc, updateRgn);
	}
}

BOOL OffscreenView::HandleEraseBkgnd(CDC* pDC) 
{
	return FALSE;
}
